package com.chart.common.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 操作json工具类
 * @author ljq
 */
public class ChartDataUtils {

    /**
     * 循环处理数据 用于心电图
     * @param dataset
     * @param lists
     * @param objectMapper
     * @return
     */
    public static ArrayNode updateDatasetSourcesCyclic(ArrayNode dataset, List<List<String>> lists, ObjectMapper objectMapper) {
        // 清空当前的dataset
        dataset.removeAll();

        // 第一行是标题
        List<String> headers = lists.get(0);
        // Loop through each header, skipping the first as it's the "X轴/数据名"
        for (int i = 1; i < headers.size(); i++) {
            ObjectNode datasetObject = objectMapper.createObjectNode();
            ArrayNode sourceArray = objectMapper.createArrayNode();

            // 添加标题行
            ArrayNode headerArray = objectMapper.createArrayNode();
            headerArray.add(headers.get(0)); // "X轴/数据名"
            headerArray.add(headers.get(i)); // "折线一", "折线二", "折线三", "折线四"
            sourceArray.add(headerArray);

            // 添加数据行
            for (int j = 1; j < lists.size(); j++) {
                List<String> row = lists.get(j);
                ArrayNode dataArray = objectMapper.createArrayNode();
                dataArray.add(row.get(0)); // X轴数据
                dataArray.add(row.get(i)); // 对应的折线数据
                sourceArray.add(dataArray);
            }

            // 设置source
            datasetObject.set("source", sourceArray);
            datasetObject.put("seriesLayoutBy", "column");
            dataset.add(datasetObject);
        }

        return dataset;
    }
    //添加空对象 可用的 13.36
    public static ArrayNode updateDatasetSources(ArrayNode dataset, List<List<String>> lists, ObjectMapper objectMapper) {
        for (int i = 0; i < dataset.size(); i++) {
            JsonNode node = dataset.get(i);
            if (node.has("source")) {
                List<List<String>> updatedSource = new ArrayList<>();
                for (List<String> list : lists) {
                    if (list.size() > i + 1) {
                        List<String> newSourceEntry = new ArrayList<>();
                        newSourceEntry.add(list.get(0));
                        newSourceEntry.add(list.get(i + 1));
                        updatedSource.add(newSourceEntry);
                    }
                }
                ((ObjectNode) node).set("source", objectMapper.valueToTree(updatedSource));
            }
        }
        return dataset;
    }


    /*    public static void updateLegendData(JsonNode legend, List<List<String>> lists) {
            if (legend.isArray() && legend.size() > 0) {
                JsonNode legendNode = legend.get(0); // 获取第一个legend对象
                // 打印更新前的 legend 数据
                if (legendNode.has("data") && !lists.isEmpty() && !lists.get(0).isEmpty()) {
                    List<String> firstRow = lists.get(0);
                    ArrayNode dataArray = (ArrayNode) legendNode.withArray("data");
                    dataArray.removeAll(); // 删除data中的原有数据
                    for (String value : firstRow) {
                        ObjectNode newDataNode = dataArray.addObject();
                        newDataNode.put("name", value);
                    }
                }
            }
        }*/
    public static void updateLegendData(JsonNode legend, List<List<String>> lists) {
        if (legend.isArray() && legend.size() > 0) {
            JsonNode legendNode = legend.get(0); // 获取第一个legend对象

            if (legendNode.has("data") && legendNode.get("data").isArray() && !lists.isEmpty() && !lists.get(0).isEmpty()) {
                ArrayNode dataArray = (ArrayNode) legendNode.withArray("data");
                dataArray.removeAll(); // 删除data中的原有数据

                List<String> firstRow = lists.get(0);
                for (String value : firstRow) {
                    ObjectNode newDataNode = dataArray.addObject();
                    newDataNode.put("name", value);
                }


            }
        }
    }

    //legend为对象
    public static void updateLegendDatas(JsonNode legend, List<List<String>> lists) {
        // 检查legend是否为对象
        if (legend.isObject()) {
            // 获取数据节点
            JsonNode dataNode = legend.get("data");
            // 确保data节点是数组且lists不为空且第一行也不为空
            if (dataNode != null && dataNode.isArray() && !lists.isEmpty() && !lists.get(0).isEmpty()) {
                // 将data节点转换为ArrayNode
                ArrayNode dataArray = (ArrayNode) dataNode;
                dataArray.removeAll(); // 删除data中的原有数据

                List<String> firstRow = lists.get(0);
                for (String value : firstRow) {
                    ObjectNode newDataNode = dataArray.addObject();
                    newDataNode.put("name", value);
                }
            }
        } else if (legend.isArray() && legend.size() > 0) {
            // 如果legend是数组，处理第一个legend对象
            JsonNode legendNode = legend.get(0); // 获取第一个legend对象

            if (legendNode.has("data") && legendNode.get("data").isArray() && !lists.isEmpty() && !lists.get(0).isEmpty()) {
                ArrayNode dataArray = (ArrayNode) legendNode.withArray("data");
                dataArray.removeAll(); // 删除data中的原有数据

                List<String> firstRow = lists.get(0);
                for (String value : firstRow) {
                    ObjectNode newDataNode = dataArray.addObject();
                    newDataNode.put("name", value);
                }
            }
        }
    }






    public static void updateLegendDataHertA(JsonNode legendArray, List<List<String>> lists, ObjectMapper objectMapper) {
        System.out.println("Initial Legend: " + legendArray.toString());

        if (legendArray.isArray()) {
            for (JsonNode legend : legendArray) {
                // 获取 legend 中的 data 数组
                JsonNode dataNode = legend.path("data");

                if (dataNode.isMissingNode()) {
                    // 如果 data 节点缺失，创建一个新的 ArrayNode 并将其设置到 legend 中
                    ArrayNode newDataNode = objectMapper.createArrayNode();
                    ((ObjectNode) legend).set("data", newDataNode);
                    dataNode = newDataNode;
                }

                if (dataNode.isArray()) {
                    ArrayNode legendData = (ArrayNode) dataNode;

                    // 清空 legendData 中的 name 值
                    for (JsonNode legendItem : legendData) {
                        ((ObjectNode) legendItem).remove("name");
                    }

                    // 把 lists 里面第一个数组的每个元素分别放进 legendData 的 name 值中
                    List<String> firstList = lists.isEmpty() ? Collections.emptyList() : lists.get(0);
                    for (int i = 1; i < firstList.size(); i++) { // 从第二个元素开始
                        if (i - 1 < legendData.size()) {
                            ((ObjectNode) legendData.get(i - 1)).put("name", firstList.get(i));
                        } else {
                            ObjectNode newLegendItem = objectMapper.createObjectNode();
                            newLegendItem.put("name", firstList.get(i));
                            legendData.add(newLegendItem);
                        }
                    }
                } else {
                    System.out.println("The 'data' field in the legend is not an array.");
                }
            }
        } else {
            System.out.println("The 'legend' field is not an array.");
        }
    }




    public static void updateLegendDataHert(JsonNode legend, List<List<String>> lists, ObjectMapper objectMapper) {
        System.out.println("Initial Legend: " + legend.toString());

        // 获取 legend 中的 data 数组
        ArrayNode legendData = (ArrayNode) legend.path("data");

        // 清空 legendData 中的 name 值
        for (JsonNode legendItem : legendData) {
            ((ObjectNode) legendItem).remove("name");
        }

        // 把 lists 里面第一个数组的每个元素分别放进 legendData 的 name 值中
        List<String> firstList = lists.isEmpty() ? Collections.emptyList() : lists.get(0);
        for (int i = 1; i < firstList.size(); i++) { // 从第二个元素开始
            if (i - 1 < legendData.size()) {
                ((ObjectNode) legendData.get(i - 1)).put("name", firstList.get(i));
            } else {
                ObjectNode newLegendItem = objectMapper.createObjectNode();
                newLegendItem.put("name", firstList.get(i));
                legendData.add(newLegendItem);
            }
        }

    }

    public static List<List<String>> extractDataFromList(List<Map<String, Object>> dataList) {
        List<List<String>> extractedData = new ArrayList<>();
        for (Map<String, Object> row : dataList) {
            List<String> rowData = new ArrayList<>();
            for (Map.Entry<String, Object> entry : row.entrySet()) {
                rowData.add(entry.getValue().toString());
            }
            extractedData.add(rowData);
        }
        return extractedData;
    }


    public static ArrayNode updateDatasetSourcesA(ArrayNode dataset, List<List<String>> lists, ObjectMapper objectMapper) {
        // 确保 dataset 和 lists 中的对象数量一致
        while (dataset.size() > lists.size()) {
            dataset.remove(dataset.size() - 1);
        }
        while (dataset.size() < lists.size()) {
            dataset.add(objectMapper.createObjectNode());
        }

        for (int i = 0; i < dataset.size(); i++) {
            JsonNode node = dataset.get(i);
            if (node.has("source")) {
                List<List<String>> updatedSource = new ArrayList<>();
                for (List<String> list : lists) {
                    if (list.size() > i + 1) {
                        List<String> newSourceEntry = new ArrayList<>();
                        newSourceEntry.add(list.get(0));
                        newSourceEntry.add(list.get(i + 1));
                        updatedSource.add(newSourceEntry);
                    }
                }
                ((ObjectNode) node).set("source", objectMapper.valueToTree(updatedSource));
            }
        }
        return dataset;
    }


}
